<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	.tree-container{max-width: 100%;max-height: 100%}
	.checkbox-item{margin-right: 15px;position: relative;top: 8px}
	.tree .tree-item{max-width: 100%;margin-right: 19px}
	.menu-edit, .menu-delete, .menu-add-sub{float: right;margin-left: 15px}
	#new-menu{margin-bottom: 10px}
</style>

<div class="page-header">
	<h1>权限菜单管理</h1>
</div>
<div class="col-xs-12">
	<button class="btn btn-primary" id="new-menu">
		<i class="ace-icon fa fa-plus"></i> 添加菜单
	</button>
</div>
<div class="col-xs-12 col-sm-5">
	<div class="search-area well well-sm">
		<h4 class="blue smaller">
			<i class="fa fa-tags"></i> <?=get_user_type_name(USER_TYPE_SUPER_ADMIN)?>菜单预览
		</h4>

		<div class="tree-container">
			<ul id="cat-tree"></ul>
		</div>
	</div>

	<div class="search-area well well-sm">
		<h4 class="blue smaller">
			<i class="fa fa-tags"></i> <?=get_user_type_name(USER_TYPE_SCHOOL)?>菜单预览
		</h4>

		<div class="tree-container">
			<ul id="cat-tree2"></ul>
		</div>
	</div>
</div>

<div class="col-xs-12 col-sm-7 hidden" id="menu-form-div">
	<div class="search-area well well-sm">
		<h4 class="blue smaller">
			<i class="fa fa-tags"></i> 菜单编辑
		</h4>
		<form class="form-horizontal" role="form" id="menu-form">
			<input type="hidden" name="id" value="" />
			<input type="hidden" name="menu_pid" value="" />
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 上级菜单：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" disabled ="" id="parent_menu" value="一级菜单" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 菜单名称：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="menu_name" placeholder="请填写菜单名称"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 菜单描述：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="menu_desc" placeholder="请填写菜单描述内容"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 所属权限：</label>
				<div class="col-sm-6">
					<label class="inline checkbox-item">
						<input class="ace" type="checkbox" name="menu_role_type" value="<?=USER_TYPE_SUPER_ADMIN?>" />
						<span class="lbl"> <?=get_user_type_name(USER_TYPE_SUPER_ADMIN)?></span>
					</label>
					<label class="inline checkbox-item">
						<input class="ace" type="checkbox" name="menu_role_type" value="<?=USER_TYPE_SCHOOL?>">
						<span class="lbl"> <?=get_user_type_name(USER_TYPE_SCHOOL)?></span>
					</label>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 菜单地址：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="menu_url" placeholder="请填写菜单地址" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 菜单图标：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="menu_icon" placeholder="请填写菜单图标" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right"> 菜单排序：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" name="menu_sort" placeholder="请填写菜单排序，从高往低排序"/>
				</div>
			</div>
			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="submit">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
					<button class="btn" type="reset">
						<i class="ace-icon fa fa-undo bigger-110"></i> 清空
					</button>
				</div>
			</div>
		</form>
	</div>
</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){

		// 管理员菜单
		var adminSource = function(options, callback){
			var $data = null
			if(!("text" in options) && !("type" in options)){
				$data = <?=$adminMenu?>;//the root tree
				callback({ data: $data });
				return;
			} else if("type" in options && options.type == "folder") {
				if("additionalParameters" in options && "children" in options.additionalParameters)
					$data = options.additionalParameters.children || {};
				else $data = {}//no data
			}
			callback({ data: $data })
		}

		// 用户菜单
		var userSource = function(options, callback){
			var $data = null
			if(!("text" in options) && !("type" in options)){
				$data = <?=$userMenu?>;//the root tree
				callback({ data: $data });
				return;
			} else if("type" in options && options.type == "folder") {
				if("additionalParameters" in options && "children" in options.additionalParameters)
					$data = options.additionalParameters.children || {};
				else $data = {}//no data
			}
			callback({ data: $data })
		}
		$('#cat-tree').ace_tree({
			dataSource: adminSource,
			'itemSelect' : false,
			'folderSelect': true,
		}).tree("discloseVisible");
		
		$('#cat-tree2').ace_tree({
			dataSource: userSource,
			'itemSelect' : false,
			'folderSelect': true,
		}).tree("discloseVisible");
		
		// 表单
		var menuForm = $("#menu-form");
		var menuSubmitUrl = "<?=admin_url("admin/menu_submit_action")?>";

		// 新增菜单
		$("#new-menu").click(function(){
			menuForm[0].reset();
			menuForm.find("[name=id],[name=menu_pid]").val(0);
			menuForm.find("#parent_menu").val('一级菜单');

			// 显示
			$("#menu-form-div").removeClass("hidden");
		})

		// 删除菜单
		$("body").on("click", ".menu-delete", function(e){
			var id = $(this).closest("li").attr("id");
			layer.confirm('确定删除该菜单吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('admin/menu_delete_action')?>";
				var postData = {"id": id};
				ajax_post(postUrl, postData, function(res){
					window.location.reload();
				})
			});
		})

		// 编辑菜单
		$("body").on("click", ".menu-edit", function(){
			var id = $(this).closest("li").attr("id");
			// 获取菜单
			var getUrl = "<?=admin_url('admin/get_menu_info')?>";
			var getData = {"id": id};
			ajax_get(getUrl, getData, function(res){
				var menu = res.data;

				// 赋值
				menuForm.find("[name=id]").val(id);
				menuForm.find("[name=menu_pid]").val(menu.menu_pid);
				menuForm.find("#parent_menu").val(menu.parentName);
				menuForm.find("[name=menu_name]").val(menu.menu_name);
				menuForm.find("[name=menu_desc]").val(menu.menu_desc);
				// 权限组
				var role = menu.menu_role_type.split(",");
				menuForm.find("[name=menu_role_type]").prop("checked", false);
				for(var i in role) menuForm.find("[name=menu_role_type][value="+ role[i] +"]").prop("checked", true);
				menuForm.find("[name=menu_url]").val(menu.menu_url);
				menuForm.find("[name=menu_icon]").val(menu.menu_icon);
				menuForm.find("[name=menu_sort]").val(menu.menu_sort);

				// 显示
				$("#menu-form-div").removeClass("hidden");
			})
		})

		// 新增子菜单
		$("body").on("click", ".menu-add-sub", function(){
			var id = $(this).closest("li").attr("id");
			menuForm[0].reset();
			menuForm.find("[name=id]").val(0);
			menuForm.find("[name=menu_pid]").val(id);
			menuForm.find("#parent_menu").val($(this).prevAll("span").text());

			// 显示
			$("#menu-form-div").removeClass("hidden");
		})

		// 提交表单
		form_submit(menuForm, menuSubmitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})
	})
</script>